package com.wandian.cqcsk.mvvm.mine.everydayenglish;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.bean.EveryDayEnglishBean;

import java.util.Calendar;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_EVERY_DAY_ENGLISH;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description EveryDayEnglishViewModel
 **/
public class EveryDayEnglishViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApi;
    private String nowData="2020-01-01";
    public MutableLiveData<EveryDayEnglishBean.NewslistEntity> englishBean = new MutableLiveData<>();
    public String english_audio="";
    public EveryDayEnglishViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }

    //获取每日英语
    public void getEnglishByDay(){
        String timeData = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("TodayEnglishData");
        Calendar calendars = Calendar.getInstance();
        nowData = calendars.get(Calendar.YEAR)+"-"+ calendars.get(Calendar.MONTH)+"-"+calendars.get(Calendar.DAY_OF_MONTH);
        if(!UIUtils.isEmpty(timeData) && timeData.equals(nowData)){
            String jsonData = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("TodayEnglishJson");
            if(!UIUtils.isEmpty(jsonData)){
                EveryDayEnglishBean bean = JsonUtil.parseJsonToBean(jsonData,EveryDayEnglishBean.class);
                if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && bean.getNewslist().size()>0){
                    english_audio = bean.getNewslist().get(0).getTts();
                    englishBean.setValue(bean.getNewslist().get(0));
                }else {
                    getData();
                }
            }else {
                getData();
            }
        }else {
            AppContext.getInstance().getAppPreferencesHelper().setKeyValue("TodayEnglishData",nowData);
            getData();
        }

    }

    private void getData(){
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("date",nowData);
        hashMap.put("rand","0");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTTDataBuyKey(NEWS_EVERY_DAY_ENGLISH,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            Log.d("getTTDataBuyKey", JsonUtil.toJson(o)+"");
                            EveryDayEnglishBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),EveryDayEnglishBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && bean.getNewslist().size()>0){
                                english_audio = bean.getNewslist().get(0).getTts();
                                englishBean.setValue(bean.getNewslist().get(0));
                                AppContext.getInstance().getAppPreferencesHelper().setKeyValue("TodayEnglishJson",JsonUtil.toJson(o)+"");
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                        ToastUtil.show("网络错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
